﻿
<script type="text/javascript">
    $(function () {
        getButton("progquestion");
    });

    function ui_progquestion_init_list(toolbar) {
        $("#ui_progquestion_dg").datagrid({       //初始化datagrid
            url: "ashx/rm_progquestion.ashx?action=search",
            striped: true, rownumbers: true, pagination: true, pageSize: 20,
            idField: 'id',
            sortName: 'id',
            sortOrder: 'desc',
            pageList: [20, 40, 60, 80, 100],
            frozenColumns: [[
                { field: 'ck', checkbox: true },
                {
                    field: 'title', title: '试题名称', width: 300, sortable: true,
                    formatter: function (value, row, index) {
                        value = $('<div>' + value + '</div>').text();
                        return value.length > 100 ? '<span title="' + value + '">' + value.substring(0, 100) + ' </span> ...' : value;
                    }
                }
            ]],
            columns: [[                
                {
                    field: 'remark', title: '试题内容', width: 300, sortable: false,
                    formatter: function (value, row, index) {
                        value = $('<div>'+value+'</div>').text();
                        return value.length > 100 ? '<span title="' + value + '">' + value.substring(0, 100) + ' </span> ...' : value;
                    }
                },
                
                { field: 'sorter', title: '排序', width: 80, sortable: true },
                { field: 'point', title: '分值', width: 80, sortable: true },
                { field: 'userId', title: '用户编号', width: 80, sortable: true }
            ]],
            toolbar: toolbar.length == 0 ? null : toolbar            
        });
    }
    function ui_progquestion_add() {
        $("<div/>").dialog({
            id: "ui_progquestion_add_dialog",
            href: "html/ui_progquestion_edit.html",
            title: "添加试题",
            iconCls: 'icon-add',
            height: 500,
            width: 800,
            modal: true,
            buttons: [{
                id: "ui_progquestion_add_btn",
                text: '添 加',
                title: "添加试题",
                iconCls: 'icon-add',
                handler: function () {
                    var sorter = $('#ui_progquestion_sorter').numberbox('getValue');
                    if (!sorter || sorter.length == 0) {                        
                        $.show_alert("提示", "请输入试题序号！");
                        return;
                    }
                    var point = $('#ui_progquestion_point').numberbox('getValue');
                    if (!point || point.length == 0) {                        
                        $.show_alert("提示", "请输入试题分值！");
                        return;
                    }
                    var title = $('#ui_progquestion_title').textbox('getValue');
                    if (!title || title.length == 0) {                        
                        $.show_alert("提示", "请输入试题标题！");
                        return;
                    }

                    var hasremark = UE.getEditor('ui_progquestion_remark_editor').hasContents();
                    var remark = UE.getEditor('ui_progquestion_remark_editor').getContent();
                    if (!hasremark) {
                        $.show_alert("提示", "请输入试题描述！");
                        return;
                    }
                    var input = $('#ui_progquestion_input').val();
                    if (!input || input.length == 0) {
                        $.show_alert("提示", "请输入试题输入数据！");
                        return;
                    }
                    var output = $('#ui_progquestion_output').val();
                    if (!output || output.length == 0) {
                        $.show_alert("提示", "请输入试题输出数据！");
                        return;
                    }
                    para = {};
                    para.action = 'add';
                    para.timespan = new Date().getTime();
                    para.title = title;
                    para.remark = remark;
                    para.sorter = sorter;
                    para.point = point;
                    para.input = input;
                    para.output = output;
                    $.ajax({
                        url: "ashx/rm_progquestion.ashx",
                        data: para,
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                UE.getEditor('ui_progquestion_remark_editor').destroy();                                
                                $("#ui_progquestion_add_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", "添加成功！");
                                $("#ui_progquestion_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');

                            } else {
                                $.show_warning("提示", data.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {                
                UE.getEditor('ui_progquestion_remark_editor');
            },
            onClose: function () {
                UE.getEditor('ui_progquestion_remark_editor').destroy();
                $("#ui_progquestion_add_dialog").dialog('destroy');
            }
        });
    }
    function ui_progquestion_edit() {
        var rows = $("#ui_progquestion_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要修改的按钮或者双击某个项目");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "不支持批量修改");
            $("#ui_progquestion_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        var row = rows[0];
        $("<div/>").dialog({
            id: "ui_progquestion_edit_dialog",
            href: "html/ui_progquestion_edit.html",
            title: "修改试题",
            iconCls: 'icon-edit',
            height: 500,
            width: 800,
            modal: true,
            buttons: [{
                id: "ui_progquestion_edit_btn",
                iconCls: 'icon-edit',
                text: '修 改',
                handler: function () {
                    var id = $('#ui_progquestion_id').val();
                    var sorter = $('#ui_progquestion_sorter').numberbox('getValue');
                    if (!sorter || sorter.length == 0) {
                        $.show_alert("提示", "请输入试题序号！");
                        return;
                    }
                    var point = $('#ui_progquestion_point').numberbox('getValue');
                    if (!point || point.length == 0) {
                        $.show_alert("提示", "请输入试题分值！");
                        return;
                    }
                    var title = $('#ui_progquestion_title').textbox('getValue');
                    if (!title || title.length == 0) {
                        $.show_alert("提示", "请输入试题标题！");
                        return;
                    }

                    var hasremark = UE.getEditor('ui_progquestion_remark_editor').hasContents();
                    var remark = UE.getEditor('ui_progquestion_remark_editor').getContent();
                    if (!hasremark) {
                        $.show_alert("提示", "请输入试题描述！");
                        return;
                    }
                    var input = $('#ui_progquestion_input').val();
                    if (!input || input.length == 0) {
                        $.show_alert("提示", "请输入试题输入数据！");
                        return;
                    }
                    var output = $('#ui_progquestion_output').val();
                    if (!output || output.length == 0) {
                        $.show_alert("提示", "请输入试题输出数据！");
                        return;
                    }
                    para = {};
                    para.action = 'edit';
                    para.timespan = new Date().getTime();
                    para.id = id;
                    para.title = title;
                    para.remark = remark;
                    para.sorter = sorter;
                    para.point = point;
                    para.input = input;
                    para.output = output;
                    $.ajax({
                        url: "ashx/rm_progquestion.ashx",
                        data: para,
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                UE.getEditor('ui_progquestion_remark_editor').destroy();
                                $("#ui_progquestion_edit_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", "修改成功！");
                                $("#ui_progquestion_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');

                            } else {
                                $.show_warning("提示", data.msg);
                            }
                        }
                    });
                }
            }],            
            onLoad: function () {
                $('#ui_progquestion_id').val(row.id);
                $('#ui_progquestion_title').textbox('setValue', row.title);
                $('#ui_progquestion_sorter').numberbox('setValue', row.sorter);
                $('#ui_progquestion_point').numberbox('setValue', row.point);
                $('#ui_progquestion_input').val(row.input);
                $('#ui_progquestion_output').val(row.output);
                var ueRemark=UE.getEditor('ui_progquestion_remark_editor');
                     
                ueRemark.addListener("ready", function () {
                    ueRemark.setContent(row.remark);
                    ueRemark.focus();
                });               
            },
            onClose: function () {
                UE.getEditor('ui_progquestion_remark_editor').destroy();
                $("#ui_progquestion_edit_dialog").dialog('destroy');
            }
        });
    }
    function ui_progquestion_delete() {
        var rows = $("#ui_progquestion_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要删除的按钮");
            return;
        }
        $.messager.confirm('提示', '确定删除勾选的这' + rows.length + '个按钮？', function (r) {
            if (r) {
                para = {};
                para.action = "delete";
                para.timespan = new Date().getTime();

                var ids = [];
                $.each(rows, function (i, row) {
                    ids.push(row.id);
                });
                para.ids = ids.join(',');
                $.ajax({
                    url: "ashx/rm_progquestion.ashx",
                    data: para,
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        if (data.success) {
                            $.show_warning("提示", "删除成功！");
                            $("#ui_progquestion_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                        } else {
                            $.show_warning("提示", data.msg);
                        }
                    }
                });
            }
        });
    }

    //上传导入数据
    function ui_progquestion_import() {

        $("<div/>").dialog({
            id: "ui_progquestion_import_dialog",
            href: "html/ui_progquestion_import.html",
            iconCls: "icon-filter",
            title: '数据导入',
            height: 220,
            width: 420,
            modal: true,
            buttons: [{
                id: "ui_progquestion_import_btn",
                text: '导入',
                iconCls: "icon-import",
                handler: function () {
                    var v = $("#ui_progquestion_import_file").val();
                    //判断是否选择图片  
                    if (v == null || v == "") {
                        //$.messager.alert('提示', '请选择要导入的文件!');
                        $("#ui_progquestion_import_file").focus();
                        return;
                    }
                    $('#ui_progquestion_import_btn').linkbutton('disable');
                    var options = {
                        type: "POST",
                        url: 'ashx/rm_progquestion.ashx',
                        data: { action: 'import', t: new Date().getTime() },
                        success: function (data) {                            
                            $('#ui_progquestion_importform_p').progressbar('setValue', 100);
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_progquestion_import_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_progquestion_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                
                                $('#ui_progquestion_importform_msg').html($('<div/>').html(dataJson.msg).text());                                
                            }
                            $('#ui_progquestion_import_btn').linkbutton('enable');  //恢复按钮
                        }
                    };
                    // 将options传给ajaxForm  
                    ui_progquestion_start();
                    $('#ui_progquestion_importform').ajaxSubmit(options);                    
                }
            }],
            onLoad: function () {
            },
            onClose: function () {
                $("#ui_progquestion_import_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
</script>
<div id="ui_progquestion_layout" class="easyui-layout" data-options="fit:true,border:false">    
    <div data-options="region:'center',border:false">
        <table id="ui_progquestion_dg" data-options="fit:true,border:false">
        </table>
    </div>
</div>
